home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 01 - 1984 & 1985 / 01.03 Feb 85.sit / 01.03 Feb 85 / Pascal Vol. 1 No. 3 / Regions
Encoding:
Text File  |  1984-12-19  |  6.3 KB  |  288 lines  |  [TEXT/PASC]

  1. program Regions;
  2.  
  3. { Regions is a MacPascal demostration program}
  4. { that provides six examples of the workings of}
  5. { the regions of QuickDraw.    }
  6. {  -- by Chris Derossi}
  7.  
  8.  uses
  9.   QuickDraw2;  {QuickDraw2 contains the stuff for regions.}
  10.  
  11.  const
  12. { These constants determine the amount and density of the}
  13. { words used to show region clipping}
  14.  
  15.   WordsPerLine = 6;
  16.   NumLines = 10;
  17.   LineHite = 15;
  18.  
  19.  var
  20.   My_Rgn : RgnHandle;  { This is our working region, used everywhere}
  21.   Big_Rect : Rect;  { This is a rectangle that is the whole drawing window}
  22.  
  23.  procedure SetUp;
  24.  
  25. { SetUp clears the screen to a full sized drawing window and uses}
  26. { DrawLine to mark it off into six sections. It also inits our region}
  27. { and sets Big_Rect to the full window.}
  28.  
  29.  begin
  30.   HideCursor;
  31.   HideAll;
  32.   SetRect(Big_Rect, 0, 20, 527, 357); {Full screen size}
  33.   SetDrawingRect(Big_Rect);
  34.   ShowDrawing;                      {Show only the drawing window}
  35.   DrawLine(0, 149, 527, 149);
  36.   DrawLine(176, 0, 176, 337);
  37.   DrawLine(352, 0, 352, 337);
  38.   My_Rgn := NewRgn;        {Init our working region}
  39.  end;
  40.  
  41.  procedure DrawWords (X, Y : integer);
  42.  
  43. { DrawWords draw several lines of words starting at (X,Y) according}
  44. { to globally declared constants}
  45.  
  46.   var
  47.    s : string;
  48.    a, b : integer;
  49.  
  50.  begin
  51.   s := 'Regions ';
  52.   for a := 0 to NumLines do
  53.    begin
  54.     MoveTo(X, Y + a * LineHite);  {Start a new line in column Y}
  55.     for b := 1 to WordsPerLine do
  56.      DrawString(s);
  57.    end;
  58.  end;
  59.  
  60.  procedure Use_Region;
  61.  
  62. { This procedure draws an outline of our region, then shrinks it so that}
  63. {the outline is not in the region, and sets the drawing window's clipping}
  64. {region to equal our region}
  65.  
  66.  begin
  67.   FrameRgn(My_Rgn);        { Draw the outline of our region}
  68.   InsetRgn(My_Rgn, 1, 1);  { Shrink it}
  69.   SetClip(My_Rgn);            { Set the drawing window's clipping region}
  70.  end;
  71.  
  72.  procedure ClearRgn;
  73.  
  74. { In order to use the entire drawing window, this procedure sets the}
  75. {clipping region to the whole window, using Big_Rect}
  76.  
  77.  begin
  78.   RectRgn(My_Rgn, Big_Rect); {Make our region a rectangle, screen size.}
  79.   SetClip(My_Rgn);
  80.  end;
  81.  
  82.  procedure DoRegion1;
  83.  
  84. { This creates the first region, which is just a circle}
  85.  
  86.   var
  87.    r : Rect;
  88.  
  89.  begin
  90.   ClearRgn;
  91.   MoveTo(25, 140);
  92.   DrawString('Simple Region');
  93.  
  94.   begin  {Create a region}
  95.    OpenRgn;
  96.    SetRect(r, 20, 20, 120, 120);
  97.    FrameOval(r);
  98.    CloseRgn(My_Rgn);
  99.   end;
  100.  
  101.   Use_Region;
  102.   DrawWords(0, 0);
  103.  end;
  104.  
  105.  procedure DoRegion2;
  106.  
  107. { This is region number 2. This is a concatination of 3 shapes.}
  108.  
  109.   var
  110.    r : Rect;
  111.  
  112.  begin
  113.   ClearRgn;
  114.   MoveTo(225, 140);
  115.   DrawString('Any Shape');
  116.  
  117.   OpenRgn;   {Start a new region}
  118.   SetRect(r, 220, 65, 300, 85);
  119.   FrameRect(r);
  120.   SetRect(r, 180, 30, 220, 120);
  121.   FrameOval(r);
  122.   SetRect(r, 300, 30, 340, 120);
  123.   FrameOval(r);
  124.   CloseRgn(My_Rgn);
  125.  
  126.   Use_Region;
  127.   DrawWords(176, 0);
  128.  end;
  129.  
  130.  procedure DoRegion3;
  131.  
  132. { Region number 3. Two overlapping regions form a 'hole'.}
  133.  
  134.   var
  135.    r : Rect;
  136.  
  137.  begin
  138.   ClearRgn;
  139.   MoveTo(410, 140);
  140.   DrawString('Holes');
  141.  
  142.   OpenRgn;
  143.   SetRect(r, 380, 20, 480, 120);
  144.   FrameOval(r);
  145.   SetRect(r, 410, 50, 450, 90);     {Completely inside the first shape}
  146.   FrameRoundRect(r, 18, 18);
  147.   CloseRgn(My_Rgn);
  148.  
  149.   Use_Region;
  150.   DrawWords(352, 0);
  151.  end;
  152.  
  153.  procedure DoRegion4;
  154.  
  155. { Region 4. This creates a region which shows the effect of }
  156. {partially overlapping region areas.}
  157.  
  158.   var
  159.    r : Rect;
  160.  
  161.  begin
  162.   ClearRgn;
  163.   MoveTo(40, 310);
  164.   DrawString('Overlapping');
  165.  
  166.   OpenRgn;
  167.   SetRect(r, 30, 180, 130, 280);
  168.   FrameOval(r);    { Draw a simple circle }
  169.   SetRect(r, 5, 215, 155, 245);
  170.   FrameRect(r);    { Draw a rectangle over the circle }
  171.   CloseRgn(My_Rgn);
  172.  
  173.   Use_Region;
  174.   DrawWords(0, 149);
  175.  end;
  176.  
  177.  procedure DoRegion5;
  178.  
  179. { Region 5. Regions do not have to be contiguous. This procedure creates}
  180. {a region with three separate areas.}
  181.  
  182.   var
  183.    r : Rect;
  184.  
  185.  begin
  186.   ClearRgn;
  187.   MoveTo(220, 310);
  188.   DrawString('Disjoint Areas');
  189.  
  190.   OpenRgn;
  191.   SetRect(r, 190, 160, 270, 230);
  192.   FrameOval(r);
  193.   SetRect(r, 280, 160, 340, 240);
  194.   FrameRect(r);
  195.   SetRect(r, 190, 255, 340, 285);
  196.   FrameRoundRect(r, 18, 18);
  197.  
  198.   CloseRgn(My_Rgn);
  199.   Use_Region;
  200.   DrawWords(176, 150);
  201.  end;
  202.  
  203.  procedure DoRegion6;
  204.  
  205. { Region 6. This procedure illustrates the effect of continuous clipping}
  206. {associated with any drawing, even animation.}
  207.  
  208.   var
  209.    r : Rect;
  210.  
  211.   procedure Animate;
  212.  
  213. { Animate, called only by DoRegion6, bounces a ball in the general area}
  214. {of region 6, showing that the clipping occurs constantly, with now need}
  215. {for special instructions. The animation stops when the mouse button is}
  216. {pressed.}
  217.  
  218.    var
  219.     X, Y, dX, dY : integer;
  220.  
  221.   begin
  222.    X := 353;  { Arbitrary starting position. }
  223.    Y := 220;
  224.    dX := 3;    { Arbitrary beginning velocity. }
  225.    dY := 0;
  226.    repeat
  227.     PenPat(white);    { Use white for drawing to }
  228.     PaintCircle(X, Y, 5);  { erase ball at current position }
  229.     X := X + dX;  { upgrade position }
  230.     Y := Y + dY;
  231.     if (X < 353) or (X > 510) or (Y < 175) or (y > 265) then
  232.      begin  { The ball has hit a "wall" and should bounce }
  233.       X := X - dX;  { Move the ball back to its last legal position }
  234.       Y := Y - dY;
  235.       repeat
  236.        dX := ((random mod 3) - 1) * 7;  { Choose new random velocities,}
  237.        dY := ((random mod 3) - 1) * 7; {with each being -7,0, or 7 }
  238.       until (dX <> 0) or (dY <> 0); { zero velocity is illegal }
  239.      end
  240.     else  { new ball position is okay. }
  241.      begin
  242.       PenPat(black);
  243.       PaintCircle(X, Y, 5);   { Draw the ball in the new position }
  244.      end;
  245.    until Button;    { Stop when the button is pressed }
  246.   end;
  247.  
  248.  begin  { Region6 }
  249.   ClearRgn;
  250.   SetRect(r, 353, 150, 530, 290);
  251.   FillRect(r, gray);    { Use a gray background for contrast }
  252.   MoveTo(375, 310);
  253.   DrawString('Continuous Clipping');
  254.  
  255.   OpenRgn;
  256.   SetRect(r, 357, 185, 432, 255);
  257.   FrameOval(r);
  258.   SetRect(r, 432, 185, 507, 255);
  259.   FrameOval(r);
  260.   CloseRgn(My_Rgn);
  261.  
  262.   Use_Region;
  263.   SetRect(r, 353, 150, 530, 290);
  264.   FillRect(r, white);   { Erase the inside of the region, using auto-clipping}
  265.   Animate;  { Do the bouncing ball }
  266.  end;
  267.  
  268.  procedure MakeRgns;
  269.  
  270. { This is a brute force way to call all six regions, but it allows us to}
  271. {break the regions down into separate procedures.}
  272.  
  273.  begin
  274.   DoRegion1;
  275.   DoRegion2;
  276.   DoRegion3;
  277.   DoRegion4;
  278.   DoRegion5;
  279.   DoRegion6;
  280.   ClearRgn;
  281.   DisposeRgn(My_Rgn);  { Free the memory used for our region }
  282.   ShowCursor;   { We need the cursor! }
  283.  end;
  284.  
  285. begin   { Regions }
  286.  SetUp;
  287.  MakeRgns;
  288. end.